<?php
	function d($str){
		echo "<pre>";
		print_r($str);
		echo "</pre>";
	}

	function dd($str){
		echo "<pre>";
		print_r($str);
		echo "</pre>";die;
	}

	function de($str){
		echo "<pre>";
		var_dump($str);
		echo "</pre>";die;
	}

	//Generate Salt for crypt function
	function blowfishSalt($cost = 13){
	    if (!is_numeric($cost) || $cost < 4 || $cost > 31) {
	        throw new Exception("cost parameter must be between 4 and 31");
	    }
	    $rand = array();
	    for ($i = 0; $i < 8; $i += 1) {
	        $rand[] = pack('S', mt_rand(0, 0xffff));
	    }
	    $rand[] = substr(microtime(), 2, 6);
	    $rand = sha1(implode('', $rand), true);
	    $salt = '$2a$' . sprintf('%02d', $cost) . '$';
	    $salt .= strtr(substr(base64_encode($rand), 0, 22), array('+' => '.'));
	    return $salt;
	}

	// compare contant time hash crypt
	function sameCrypt($a, $b) {
	    if (!is_string($a) || !is_string($b)) {
	        return false;
	    }
	    $mb = function_exists('mb_strlen');
	    $length = $mb ? mb_strlen($a, '8bit') : strlen($a);
	    if ($length !== ($mb ? mb_strlen($b, '8bit') : strlen($b))) {
	        return false;
	    }
	    $check = 0;
	    for ($i = 0; $i < $length; $i += 1) {
	        $check |= (ord($a[$i]) ^ ord($b[$i]));
	    }
	    return $check === 0;
	}

function generateRandomString($length = 20, $letters = '1234567890qwertyuiopasdfghjklzxcvbnm') {
    $s = '';
    $lettersLength = strlen($letters) - 1;
    for ($i = 0; $i < $length; $i++) {
        $s .= $letters[rand(0, $lettersLength)];
    }
    return $s;
}
?>